/**
 * Created by oneminute on 7/28/14.
 */

var assert = require("assert");
var should = require("should");
var logger = require("../../lib/utils/logger");
var testutils = require("../testutils");
var lz = require("../../lib/utils/modules")();
var httpMocks = require("../lib/http-mock");
var async = require("async");
var path = require("path");
var delneed = require("../../routes/delneed");
var userFilter = require("../../routes/userfilter");

describe("测试routes/delneed模块。", function () {

    var user = null;
    before(function (done) {
        var uid = lz.Utils.getUUID("u_");
        var demands = [];
        for (var i = 0; i < 10; i++) {
            demands.push({
                id: lz.Utils.getUUID("d_"),
                uid: uid,
                description: "测试商品描述" + i,
                price: 101.18
            });
        }
        user = new lz.User({
            id: uid,
            name: lz.Utils.getRandName(),
            demands: demands
        });
        user.save(function (_err, _user) {
            done();
        });
    });

    it("正常删除需求。", function (done) {
        var request = httpMocks.createExpressRequest({
            method: 'GET',
            url: '/delneed?uid=' + user.id + "&nid=" + user.demands[6].id
        });
        var response = httpMocks.createExpressResponse();

        var demand7_id = user.demands[7]._id;
        var getFn = delneed.get.bind(null, request, response, {}, function (_err, _user, _args) {
            var user02 = new lz.User({_id: user._id});
            user02.load({loadAll: false}, function (_err, _user) {
                testutils.check(done, function () {
                    assert.equal(_user.demands.length, 9);
                    assert.equal(_user.demands[6].aid, demand7_id);
                });
            });
        });
        userFilter(request, response, getFn);
    });

    it("未提供有效的uid。", function (done) {
        var request = httpMocks.createExpressRequest({
            method: 'GET',
            url: '/delneed?uid=' + lz.Utils.getUUID("u_")
        });
        var response = httpMocks.createExpressResponse();

        var getFn = delneed.get.bind(null, request, response, {}, function (_err, _user, _args) {
            testutils.check(done, function () {
                assert.equal(_err.code, lz.Config.errors.REQUEST_TEMPUSER_ERROR.code);
            });
        });
        userFilter(request, response, getFn);
    });

    after(function (done) {
        user.refresh(function () {
            user.delete(function () {
                done();
            });
        });
    });
});